/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package models;
import com.sun.javadoc.Tag;
import java.util.*;
import javax.persistence.*;


import play.db.jpa.*;

import models.Comment;
import net.sf.oval.constraint.MaxSize;
import play.data.validation.Required;

/**
 *
 * @author yelinaung
 */
@Entity
public class Post extends Model {

    @Required
    public String title;

    @Required
    public Date postedAt;

    @Lob
    @Required
    @MaxSize(10000)
    public String content;

    @Required
    @ManyToOne
    public User author;

    @OneToMany(mappedBy="post", cascade=CascadeType.ALL)
    public List<Comment> comments;

    public Post(User author, String title, String content) {
        this.comments = new ArrayList<Comment>();
        this.author = author;
        this.title = title;
        this.content = content;
        this.postedAt = new Date();
        }

        public Post addComment(String author, String content) {
        Comment newComment = new Comment(this, author, content).save();
        this.comments.add(newComment);
        return this;
        }

     public Post previous() {
         return Post.find("postedAt < ? order by postedAt desc", postedAt).first();
     }

     public Post next(){
         return Post.find("postedAt > ? order by postedAt asc", postedAt).first();
     }
}